﻿package net.WMisiedjan.RedstoneEffects;

import java.io.*;
import java.util.HashMap;

import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;

/**
 * RedstoneEffects for Bukkit
 *
 * @author SERVER_PreLoaded
 */
public class RedstoneEffects extends JavaPlugin {
	private final RedstoneEffectsPlayerListener playerListener = new RedstoneEffectsPlayerListener(this);
	private final RedstoneEffectsBlockListener blockListener = new RedstoneEffectsBlockListener(this);

    public final static StackableLogger log = new StackableLogger("RedstoneEffects");
    public RedstoneEffectsConf config = new RedstoneEffectsConf(this.getDataFolder());
    public static RedstoneEffectsPermissions permissionsHandler;
    
    public static StackableLogger getLogger()
    {
    	return log;
    }

    public void onEnable() {
        PluginManager pm = getServer().getPluginManager();
    	PluginDescriptionFile pdfFile = this.getDescription();
    	RedstoneEffects.getLogger().info("Loading "+pdfFile.getName());
    	RedstoneEffects.getLogger().fine("Loading Configuration...");
    	config.load();
    	RedstoneEffects.getLogger().info("Loading Permissions...");
    	setupPermissions();

        getServer().getPluginManager().registerEvent(Event.Type.REDSTONE_CHANGE, blockListener, Priority.Normal, this);
        getServer().getPluginManager().registerEvent(Event.Type.BLOCK_PLACE, blockListener, Priority.Normal, this);
        getServer().getPluginManager().registerEvent(Event.Type.SIGN_CHANGE, blockListener, Priority.Normal, this);

		
        // EXAMPLE: Custom code, here we just output some info so we can check all is well
        
        System.out.println( pdfFile.getName() + " version " + pdfFile.getVersion() + " is enabled!" );
    }
    
    	
	private void setupPermissions() {
	    PluginManager pm = getServer().getPluginManager();
		Plugin permissionsPlugin = pm.getPlugin("Permissions");

		if (permissionsPlugin != null)
		{
			RedstoneEffects.permissionsHandler = new RedstoneEffectsPermissions(permissionsPlugin);
			
			RedstoneEffects.getLogger().fine("Detected Permissions version "+permissionsPlugin.getDescription().getVersion());
		}
		else
		{
			RedstoneEffects.getLogger().fine("No Permissions Detected! Defaulting to OP/Config files!.");
			RedstoneEffects.permissionsHandler = new RedstoneEffectsPermissions();
		}
	  }
      
    public void onDisable() {
        // TODO: Place any custom disable code here

        // NOTE: All registered events are automatically unregistered when a plugin is disabled

        // EXAMPLE: Custom code, here we just output some info so we can check all is well
    }
}

